home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Bitmap / Sources / BmpFrame.cpp next >
Encoding:
Text File  |  1995-11-08  |  16.4 KB  |  566 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                BmpFrame.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFBMap.hpp"
  13.  
  14. #ifndef BMPFRAME_H
  15. #include "BmpFrame.h"
  16. #endif
  17.  
  18. #ifndef BMPPART_H
  19. #include "BmpPart.h"
  20. #endif
  21.  
  22. #ifndef BMPSEL_H
  23. #include "BmpSel.h"
  24. #endif
  25.  
  26. #ifndef RBBRBAND_H
  27. #include "RbbrBand.h"
  28. #endif
  29.  
  30. // ----- Part Layer -----
  31.  
  32. #ifndef FWUTIL_H
  33. #include "FWUtil.h"
  34. #endif
  35.  
  36. #ifndef FWPRTITE_H
  37. #include "FWPrtIte.h"
  38. #endif
  39.  
  40. #ifndef FWGROWBX_H
  41. #include "FWGrowBx.h"
  42. #endif
  43.  
  44. #ifndef FWSCLBAR_H
  45. #include "FWSclBar.h"
  46. #endif
  47.  
  48. #ifndef FWPRESEN_H
  49. #include "FWPresen.h"
  50. #endif
  51.  
  52. #ifndef FWITERS_H
  53. #include "FWIters.h"
  54. #endif
  55.  
  56. // ----- OS Layer -----
  57.  
  58. #ifndef FWODGEOM_H
  59. #include "FWODGeom.h"
  60. #endif
  61.  
  62. #ifndef FWBMPSHP_H
  63. #include "FWBmpShp.h"
  64. #endif
  65.  
  66. #ifndef FWEVENT_H
  67. #include "FWEvent.h"
  68. #endif
  69.  
  70. #ifndef FWSUSINK_H
  71. #include "FWSUSink.h"
  72. #endif
  73.  
  74. // ----- OpenDoc Includes -----
  75.  
  76. #ifndef SOM_Module_OpenDoc_StdProps_defined
  77. #include <StdProps.xh>
  78. #endif
  79.  
  80. #ifndef SOM_ODDragItemIterator_xh
  81. #include <DgItmIt.xh>
  82. #endif
  83.  
  84. //========================================================================================
  85. //    Runtime Informations
  86. //========================================================================================
  87.  
  88. #ifdef FW_BUILD_MAC
  89. #pragma segment odfbitmap
  90. #endif
  91.  
  92. //========================================================================================
  93. //    class CBitmapFrame
  94. //========================================================================================
  95.  
  96. //----------------------------------------------------------------------------------------
  97. //    CBitmapFrame::CBitmapFrame
  98. //----------------------------------------------------------------------------------------
  99. //    CBitmapFrame constructor
  100.  
  101. CBitmapFrame::CBitmapFrame(Environment* ev, ODFrame* odFrame, FW_CPresentation* presentation, CBitmapPart* bitmapPart) :
  102.     FW_CFrame(ev, odFrame, presentation, bitmapPart),
  103.     fBitmapPart(bitmapPart),
  104.     fChoosenSize(cRealSize),
  105.     fGrowBox(NULL),
  106.     fSelection((CBitmapSelection*)presentation->GetSelection(ev))
  107. {    
  108.     // ----- Save my current frame rect -----
  109.     fFrameRect = this->GetBounds(ev);
  110.     
  111.     // ----- I want to be droppable -----
  112.     this->SetDroppable(ev, TRUE);
  113. }
  114.  
  115. //----------------------------------------------------------------------------------------
  116. //    CBitmapFrame::~CBitmapFrame
  117. //----------------------------------------------------------------------------------------
  118. //    CBitmapFrame destructor
  119.  
  120. CBitmapFrame::~CBitmapFrame()
  121. {
  122.     // ----- Nothing to do -----
  123. }
  124.  
  125. //----------------------------------------------------------------------------------------
  126. //    CBitmapFrame::DoAdjustMenus
  127. //----------------------------------------------------------------------------------------
  128. //    ODF Method
  129.  
  130. FW_Boolean CBitmapFrame::DoAdjustMenus(Environment* ev, FW_CMenuBar* menuBar, FW_Boolean hasMenuFocus, FW_Boolean isRoot)
  131. {
  132.     if (hasMenuFocus)
  133.     {
  134.         // ----- OpenDoc Items -----    
  135.         FW_Boolean hasRightProperty = HasPropertyOnClipboard(ev, kODPropContents, CBitmapPart::kPartKind);
  136.         
  137. #ifdef FW_BUILD_MAC
  138.         // ----- On the Mac I allow Paste for both my kind and PICT. On Window I only allow Paste for my kind
  139.         if (!hasRightProperty)
  140.             hasRightProperty = hasRightProperty || HasPropertyOnClipboard(ev, kODPropContents, FW_CPart::gMacPICTDataType);
  141. #endif
  142.     
  143.         menuBar->EnableCommand(ev, kODCommandSelectAll, TRUE);                        // Allow Select All
  144.         menuBar->EnableCommand(ev, kODCommandClear, FALSE);                            // Don't allow Clear
  145.         menuBar->EnableCommand(ev, kODCommandCut, FALSE);                            // Don't allow cut                        
  146.         menuBar->EnableCommand(ev, kODCommandCopy, !fSelection->IsEmpty(ev));    // Allow copy only if my selection is not empty
  147.         menuBar->EnableCommand(ev, kODCommandPaste, hasRightProperty);                // Allow Paste if right property on the clipboard
  148.         menuBar->EnableCommand(ev, kODCommandPasteAs, FALSE);                        // Don't allow Past As for now
  149.         
  150.         // ----- My Items -----
  151.         ODCommandID choosenSize = GetChoosenSize();
  152.         
  153.         menuBar->EnableAndCheckCommand(ev, cHalfSize, TRUE, fChoosenSize == cHalfSize);    
  154.         menuBar->EnableAndCheckCommand(ev, cRealSize, TRUE, fChoosenSize == cRealSize);
  155.         menuBar->EnableAndCheckCommand(ev, cDoubleSize, TRUE, fChoosenSize == cDoubleSize);
  156.         menuBar->EnableAndCheckCommand(ev, cFitToFrame, TRUE, fChoosenSize == cFitToFrame);
  157.     }
  158.     
  159.     return FALSE;
  160. }
  161.  
  162. //----------------------------------------------------------------------------------------
  163. //    CBitmapFrame::DoMenu
  164. //----------------------------------------------------------------------------------------
  165. // ODF method
  166.  
  167. FW_Boolean CBitmapFrame::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent)
  168. {
  169.     FW_Boolean result = TRUE;
  170.     ODCommandID commandID = theMenuEvent.GetCommandID(ev);
  171.  
  172.     if (commandID == cHalfSize || 
  173.         commandID == cRealSize || 
  174.         commandID == cDoubleSize || 
  175.         commandID == cFitToFrame)
  176.     {
  177.         if (commandID != fChoosenSize)
  178.         {
  179.             fChoosenSize = commandID;
  180.             fBitmapPart->AdjustFramesSize(ev);
  181.         }
  182.     }
  183.     else
  184.         result = FALSE;
  185.  
  186.     return result;
  187. }
  188.  
  189. //----------------------------------------------------------------------------------------
  190. //    CBitmapFrame::DoIdle
  191. //----------------------------------------------------------------------------------------
  192.  
  193. FW_Boolean CBitmapFrame::DoIdle(Environment* ev, const FW_CNullEvent& theNullEvent)
  194. {
  195. FW_UNUSED(theNullEvent);    
  196.     if (fSelection != NULL && !fSelection->IsEmpty(ev))
  197.         fSelection->MoveAnts(ev, this);
  198.     
  199.     return TRUE;
  200. }
  201.  
  202. //----------------------------------------------------------------------------------------
  203. //    CBitmapFrame::AdjustFrameSize
  204. //----------------------------------------------------------------------------------------
  205.  
  206. void CBitmapFrame::AdjustFrameSize(Environment* ev)
  207. {
  208.     if (IsRoot(ev))
  209.         UpdateUsedAndActiveShapes(ev);
  210.     else
  211.     {
  212.         FW_CRect usedRect;
  213.         CalcUsedRect(ev, usedRect);
  214.         usedRect.Place(FW_kZeroPoint);
  215.     
  216.         FW_CAcquiredODShape aqAskedFrameShape = ::FW_NewODShape(ev, usedRect);
  217.         FW_CAcquiredODShape aqFrameShape = AcquireFrameShape(ev);
  218.         
  219.         if (aqAskedFrameShape->IsSameAs(ev, aqFrameShape))
  220.             UpdateUsedAndActiveShapes(ev);
  221.         else
  222.             this->RequestFrameShape(ev, aqAskedFrameShape);
  223.     }
  224.     
  225.     this->Invalidate(ev);
  226. }
  227.  
  228. //----------------------------------------------------------------------------------------
  229. //    CBitmapFrame::FocusStateChanged
  230. //----------------------------------------------------------------------------------------
  231.  
  232. void CBitmapFrame::FocusStateChanged(Environment* ev, ODTypeToken focus, FW_Boolean newState, ODFrame* newOwner)
  233. {    
  234.     FW_CFrame::FocusStateChanged(ev, focus, newState, newOwner);
  235.  
  236.     if (focus == FW_CPart::gSelectionFocusToken)
  237.     {
  238.         if (!fSelection->IsEmpty(ev))
  239.             fSelection->DrawAnts(ev, this);
  240.             
  241.         if (newState)
  242.             RegisterIdle(ev, 15);
  243.         else
  244.             UnregisterIdle(ev);
  245.     }
  246. }
  247.  
  248. //----------------------------------------------------------------------------------------
  249. //    CBitmapFrame::FrameShapeChanged
  250. //----------------------------------------------------------------------------------------
  251.  
  252. void CBitmapFrame::FrameShapeChanged(Environment* ev)
  253. {    
  254.     FW_CFrame::FrameShapeChanged(ev);
  255.  
  256.     FW_CRect updateRect(fFrameRect);
  257.     
  258.     fFrameRect = GetBounds(ev);    // Get new Frame Rect
  259.     
  260.     updateRect.Union(fFrameRect);
  261.     
  262.     // ----- Force redraw of the frame shape -----
  263.     this->Invalidate(ev, updateRect);
  264. }
  265.  
  266. //----------------------------------------------------------------------------------------
  267. //    CBitmapFrame::BuildThumbnail
  268. //----------------------------------------------------------------------------------------
  269.  
  270. void CBitmapFrame::BuildThumbnail(Environment* ev, FW_CGraphicContext& gc, const FW_CRect& bounds)
  271. {
  272.     FW_PBitmap bitmap = fBitmapPart->GetBitmap(ev);
  273.     
  274.     FW_CRect dstRect;
  275.     bitmap->GetBitmapBounds(gc, dstRect);
  276.     dstRect.Place(FW_kZeroPoint);
  277.     
  278.     FW_CRect rect(bounds);
  279.     rect.Place(FW_kZeroPoint);
  280.     if (rect.right / dstRect.right < rect.bottom / dstRect.bottom)
  281.     {
  282.         dstRect.right = rect.right;
  283.         dstRect.bottom = (dstRect.bottom / dstRect.right) * rect.right;
  284.     }
  285.     else
  286.     {
  287.         dstRect.right = (dstRect.bottom / dstRect.right) * rect.bottom;
  288.         dstRect.bottom = rect.bottom;
  289.     }    
  290.         
  291.     dstRect.PlaceInCenter(bounds);
  292.     
  293.     FW_CBitmapShape::RenderBitmap(gc, bitmap, dstRect);
  294. }
  295.  
  296. //----------------------------------------------------------------------------------------
  297. //    CBitmapFrame::AdjustUsedShape
  298. //----------------------------------------------------------------------------------------
  299.  
  300. ODShape* CBitmapFrame::AdjustUsedShape(Environment* ev, ODShape* suggestedUsedShape)
  301. {
  302.     FW_CRect usedShapeRect;
  303.     CalcUsedRect(ev, usedShapeRect);
  304.     
  305.     FW_CRect frameRect = GetBounds(ev);
  306.     usedShapeRect.Intersection(frameRect);
  307.     
  308.     ODRect odUsedShapeRect = usedShapeRect;
  309.     suggestedUsedShape->SetRectangle(ev, &odUsedShapeRect);
  310.     
  311.     suggestedUsedShape->Acquire(ev);    // because I am reusing the shape passed as parameter
  312.     return suggestedUsedShape;
  313. }
  314.  
  315. //----------------------------------------------------------------------------------------
  316. //    CBitmapFrame::AdjustActiveShape
  317. //----------------------------------------------------------------------------------------
  318. //    [HLX] Because OpenDoc uses the Frame shape as the default active shape I need to 
  319. //    adjust my active shape too.
  320.  
  321. ODShape* CBitmapFrame::AdjustActiveShape(Environment* ev, ODFacet* facet, ODShape* suggestedActiveShape)
  322. {
  323.     suggestedActiveShape->Acquire(ev);
  324.     return suggestedActiveShape;
  325. }
  326.  
  327. //----------------------------------------------------------------------------------------
  328. //    CBitmapFrame::AdjustZoomedWindowSize
  329. //----------------------------------------------------------------------------------------
  330.  
  331. void CBitmapFrame::AdjustZoomedWindowSize(Environment *ev, FW_CPoint& proposedSize)
  332. {
  333.     FW_CRect usedShapeRect;
  334.     CalcUsedRect(ev, usedShapeRect);
  335.     
  336.     proposedSize.Set(usedShapeRect.Width() + FW_IntToFixed(10), usedShapeRect.Height() + FW_IntToFixed(10));
  337. }
  338.  
  339. //----------------------------------------------------------------------------------------
  340. //    CBitmapFrame::GetZoomRatio
  341. //----------------------------------------------------------------------------------------
  342.  
  343. FW_CPoint CBitmapFrame::GetZoomRatio(Environment* ev) const
  344. {
  345.     FW_PBitmap bitmap = fBitmapPart->GetBitmap(ev);
  346.     FW_CRect bounds;
  347.     bitmap->GetBitmapBounds(bounds);
  348.     
  349.     FW_CRect usedRect;
  350.     CalcUsedRect(ev, usedRect);
  351.     
  352.     FW_CPoint ratio(
  353.         (usedRect.right - usedRect.left) / bounds.right, 
  354.         (usedRect.bottom - usedRect.top) / bounds.bottom);
  355.     
  356.     return ratio;
  357. }
  358.  
  359. //----------------------------------------------------------------------------------------
  360. //    CBitmapFrame::Draw
  361. //----------------------------------------------------------------------------------------
  362.  
  363. void CBitmapFrame::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)
  364. {
  365.     FW_CViewContext vc(ev, this, odFacet, invalidShape);
  366.     
  367.     // ------ [HLX] Will be moved to an adorner
  368.     if (IsTopFrame(ev))
  369.     {
  370.         FW_CRect invalidRect;
  371.         vc.GetClipRect(invalidRect);
  372.  
  373. #ifdef FW_BUILD_MAC
  374.         FW_PInk ink(FW_kRGBWhite,
  375.                     FW_kRGBWhite,
  376.                     FW_kErase);
  377. #else
  378.         FW_PInk ink(FW_CColor(::GetSysColor(COLOR_WINDOWTEXT), 0),
  379.                     FW_CColor(::GetSysColor(COLOR_APPWORKSPACE), 0),
  380.                     FW_kErase);
  381. #endif
  382.         FW_CRectShape::RenderRect(vc, invalidRect, FW_kFill, ink);
  383.     }
  384.  
  385.     FW_CRect usedRect;
  386.     CalcUsedRect(ev, usedRect);
  387.     
  388.     // ----- Use the static rendering method -----
  389.     FW_CBitmapShape::RenderBitmap(vc, fBitmapPart->GetBitmap(ev), usedRect);
  390.     
  391.     // ----- Draw the ants if there is a selection -----
  392.     if (IsActive(ev) && !fSelection->IsEmpty(ev))
  393.     {
  394.         fSelection->DoDrawAnts(ev, vc, this);
  395.     }
  396. }
  397.  
  398. //----------------------------------------------------------------------------------------
  399. //    CBitmapFrame::CalcUsedRect
  400. //----------------------------------------------------------------------------------------
  401.  
  402. void CBitmapFrame::CalcUsedRect(Environment* ev, FW_CRect& newUsedRect) const
  403. {
  404.     ODCommandID choosenSize = GetChoosenSize();
  405.     
  406.     FW_CRect frameRect = GetBounds(ev);
  407.     frameRect.Place(FW_kZeroPoint);
  408.     
  409.     if (choosenSize == cFitToFrame)
  410.     {
  411.         newUsedRect = frameRect;
  412.     }
  413.     else
  414.     {    
  415.         fBitmapPart->GetBitmap(ev)->GetBitmapBounds(newUsedRect);    // return in 72 dpi
  416.         
  417.         if (choosenSize == cHalfSize)
  418.         {
  419.             newUsedRect.right   = newUsedRect.right.DividedByInt(2);
  420.             newUsedRect.bottom  = newUsedRect.bottom.DividedByInt(2);
  421.         }
  422.         else if (choosenSize == cDoubleSize)
  423.         {
  424.             newUsedRect.right   = newUsedRect.right.MultipliedByInt(2);
  425.             newUsedRect.bottom  = newUsedRect.bottom.MultipliedByInt(2);
  426.         }
  427.         
  428.         newUsedRect.PlaceInCenter(frameRect);
  429.     }
  430. }
  431.  
  432. //----------------------------------------------------------------------------------------
  433. //    CBitmapFrame::DoMouseDown
  434. //----------------------------------------------------------------------------------------
  435.  
  436. FW_Boolean CBitmapFrame::DoMouseDown(Environment* ev, const FW_CMouseEvent& theMouseEvent)
  437. {    
  438.     if (fSelection->IsMouseInDraggableItem(ev, this, theMouseEvent, FALSE))
  439.     {
  440.         if (!Drag(ev, theMouseEvent))
  441.             fSelection->CloseSelection(ev);
  442.     }
  443.     else
  444.     {
  445.         fSelection->CloseSelection(ev);
  446.         
  447.         FW_CRect usedRect;
  448.         CalcUsedRect(ev, usedRect);
  449.         
  450.          FW_CPoint where = theMouseEvent.GetMousePosition(ev, FW_CMouseEvent::kFrame);
  451.         FW_CFrame *frame = FW_CFrame::ODtoFWFrame(ev, theMouseEvent.GetFacet(ev)->GetFrame(ev));
  452.         frame->GetContentView(ev)->FrameToViewContent(ev, where);
  453.         
  454.         if (usedRect.Contains(where))
  455.         {
  456.             // ----- ... and start a track -----
  457.             CRectRubberBand rubberBand(ev, this, theMouseEvent.GetFacet(ev), usedRect, GetZoomRatio(ev));
  458.         
  459.             if (rubberBand.Track(ev, theMouseEvent))
  460.             {
  461.                 FW_CRect rect;
  462.                 rubberBand.GetRect(rect);
  463.                 
  464.                 // ----- Apply the zoom ratio ----
  465.                 fSelection->SetSelectRect(GetZoomRatio(ev), usedRect, rect);
  466.                 fSelection->DrawAnts(ev, this);
  467.             }
  468.         }
  469.         else
  470.             FW_Beep();
  471.     }        
  472.  
  473.     return TRUE;
  474. }
  475.  
  476. #ifdef FW_BUILD_MAC
  477. //----------------------------------------------------------------------------------------
  478. //    CBitmapFrame::CanAcceptDrop
  479. //----------------------------------------------------------------------------------------
  480.  
  481. ODDragResult CBitmapFrame::CanAcceptDrop(Environment* ev, ODDragItemIterator* dragInfo)
  482. {
  483.     ODDragResult result = FW_CFrame::CanAcceptDrop(ev, dragInfo);
  484.     
  485.     // ----- Test for PICT also -----
  486.     if (!result)
  487.     {
  488.         for (ODStorageUnit *dragSU = dragInfo->First(ev); dragSU; dragSU = dragInfo->Next(ev))
  489.             if (dragSU->Exists(ev, kODPropContents, FW_CPart::gMacPICTDataType, 0))    // 'PICT' in Scrap
  490.                 return TRUE;
  491.             else if (dragSU->Exists(ev, kODPropContents, FW_CPart::gMacPICTFileType, 0))    // PICT file
  492.                 return TRUE;
  493.     }
  494.     
  495.     return result;
  496. }
  497. #endif
  498.  
  499. //----------------------------------------------------------------------------------------
  500. // CBitmapFrame::CreateSubviews
  501. //----------------------------------------------------------------------------------------
  502.  
  503. void CBitmapFrame::CreateSubviews(Environment* ev)
  504. {
  505.     if (IsRoot(ev))
  506.     {        
  507.         FW_CPoint sbSize = FW_CScrollBar::GetDefaultScrollBarSize();
  508.     
  509.         FW_CRect frameRect = this->GetBounds(ev);
  510.         frameRect.bottom -= sbSize.y;
  511.         frameRect.right -= sbSize.x;
  512.         
  513.         // ----- Create the GrowBox gadget
  514.         fGrowBox = new FW_CGrowBox(ev, this, 0, frameRect[FW_kBotRight]);
  515.     }
  516. }
  517.  
  518. //----------------------------------------------------------------------------------------
  519. // CBitmapFrame::AdjustSubviews
  520. //----------------------------------------------------------------------------------------
  521.  
  522. void CBitmapFrame::AdjustSubviews(Environment* ev)
  523. {        
  524.     if (IsRoot(ev))
  525.     {
  526.         FW_ASSERT(fGrowBox);
  527.             
  528.         FW_CPoint sbSize = FW_CScrollBar::GetDefaultScrollBarSize();
  529.     
  530.         FW_CRect frameRect = GetBounds(ev);
  531.         frameRect.bottom -= sbSize.y;
  532.         frameRect.right -= sbSize.x;
  533.         
  534.         fGrowBox->Invalidate(ev);    // Invalidate old position
  535.         fGrowBox->SetLocation(ev, frameRect[FW_kBotRight]);
  536.         fGrowBox->Invalidate(ev);    // Invalidate new position
  537.     }
  538. }
  539.  
  540. //----------------------------------------------------------------------------------------
  541. //    CBitmapFrame::ExternalizeFrame
  542. //----------------------------------------------------------------------------------------
  543.  
  544. void CBitmapFrame::ExternalizeFrame(Environment* ev, ODStorageUnitView* storageUnitView)
  545. {
  546.     FW_CFrame::ExternalizeFrame(ev, storageUnitView);
  547.  
  548.     FW_CStorageUnitSink sink(storageUnitView);
  549.     FW_CWritableStream stream(&sink);
  550.     stream << fChoosenSize;
  551. }
  552.  
  553. //----------------------------------------------------------------------------------------
  554. //    CBitmapFrame::InternalizeFrame
  555. //----------------------------------------------------------------------------------------
  556.  
  557. void CBitmapFrame::InternalizeFrame(Environment* ev, ODStorageUnitView* storageUnitView)
  558. {
  559.     FW_CFrame::InternalizeFrame(ev, storageUnitView);
  560.     
  561.     FW_CStorageUnitSink sink(storageUnitView);
  562.     FW_CReadableStream stream(&sink);
  563.     stream >> fChoosenSize;
  564. }
  565.  
  566.